﻿/************************************************************************************
 * xujiajun@yoozoo.com 御剑
 * 显示相关的元素的抽象工厂，用于创建和销毁元素
 ***********************************************************************************/

using System.Collections.Generic;

namespace WorldMapBase.Display
{
    /// <summary>
    /// 显示相关的元素的抽象工厂，用于创建和销毁元素
    /// </summary>
    public abstract class DisplayElementFactory
    {
        private readonly HashSet<IDisplayElement> m_ElementSets = new HashSet<IDisplayElement>();

        public DisplayElementFactory()
        {
            for (int i = 0; i < 1000; i++)
            {
                m_ElementSets.Add(new EmptyDisplayElement());
            }
            m_ElementSets.Clear();
        }
        
        /// <summary>
        /// 创建指定资源ID的显示元素
        /// </summary>
        /// <param name="resID">资源ID</param>
        /// <returns>创建出的显示元素</returns>
        public IDisplayElement CreateDisplayElement(int lodIndex, int mapLayer,int resID)
        {
            var element = OnCreateDisplayElement(lodIndex,mapLayer, resID);
            if (element != null)
            {
                m_ElementSets.Add(element);
                element.OnCreate(resID);
            }
            return element;
        }

        /// <summary>
        /// 销毁指定显示元素
        /// </summary>
        /// <param name="element">显示元素</param>
        public void DestroyDisplayElement(IDisplayElement element)
        {
            m_ElementSets.Remove(element);
            element.OnDestroy();
            OnDestroyDisplayElement(element);
        }

        /// <summary>
        /// 销毁工厂
        /// </summary>
        public void Destroyfactory()
        {
            foreach (var element in m_ElementSets)
            {
                element.OnDestroy();
                OnDestroyDisplayElement(element);
            }
            m_ElementSets.Clear();
        }

        protected abstract IDisplayElement OnCreateDisplayElement(int lodIndex,int mapLayer, int resID);
        protected abstract void OnDestroyDisplayElement(IDisplayElement element);
    }

}
